package com.example.client.controller;

import cn.example.server.dto.User;
import com.example.client.service.CxfService;
import com.example.client.service.HessianService;
import com.example.client.service.RestService;
import com.example.client.service.RmiService;
import com.example.client.utils.CxfProxyFactoryUtil;
import com.example.client.utils.HessianProxyFactoryUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: YAOZUN
 * @Date: Created in 16:44 2018/12/18 0018
 * @Description:
 */
@RestController
@RequestMapping("/transmission")
public class TransmissionController {

    @Autowired
    private RmiService rmiService;
    @Autowired
    private RestService restService;

    private static final String msg = "小数据测试";
    private static final String host = "http://localhost:8080/";
    private static final String remoteHost = "http://47.107.224.239:8080/";
    @RequestMapping("/rmiService")
    public String rmiService() {
        Long startTime = System.currentTimeMillis();
        String test = rmiService.invokingRemoteService(msg);
        Long time = System.currentTimeMillis() - startTime;
        String response = "返回内容："+test+"Rmi调用耗时："+time;
        return response;
    }

    @RequestMapping("/hessianService")
    public String hessianService() {
        Long startTime = System.currentTimeMillis();
        String test = "";
        try {
            HessianService helloHessian = HessianProxyFactoryUtil.getHessianClientBean(HessianService.class,host+"hessianTest");
            test =  helloHessian.SayHello(msg);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Long time = System.currentTimeMillis() - startTime;
        String response = "返回内容："+test+"Hessian调用耗时："+time;
        return response;
    }

    @RequestMapping("/cxfService")
    public String cxfService() {
        Long startTime = System.currentTimeMillis();
        String test = "";
        try{
            CxfService cxfService = CxfProxyFactoryUtil.getCxfClientBean(CxfService.class,host+"soap/user?wsdl");
            test = cxfService.sayHello(msg);
        }catch (Exception e){
            e.printStackTrace();
        }
        Long time = System.currentTimeMillis() - startTime;
        String response = "返回内容："+test+"Cxf调用耗时："+time;
        return response;
    }

    @RequestMapping("/restGetService")
    public String restGetService(){
        Long startTime = System.currentTimeMillis();
        String test = restService.getRest(host+"transmission/getRest?msg="+msg);
        Long time = System.currentTimeMillis() - startTime;
        String response = "返回内容："+test+"restGet调用耗时："+time;
        return response;
    }

    @RequestMapping("/restPostService")
    public String restPostService(){
        Long startTime = System.currentTimeMillis();
        String test = restService.postRest(msg,host+"transmission/postRest");
        Long time = System.currentTimeMillis() - startTime;
        String response = "返回内容："+test+"restPost调用耗时："+time;
        return response;
    }

    @RequestMapping("/rmiServiceBigMsg")
    public String rmiServiceBigMsg(){
        Long startTime = System.currentTimeMillis();
        List<User> users = rmiService.bigMsg();
        Long time = System.currentTimeMillis() - startTime;
        String response = "返回内容："+users.size()+"Rmi调用耗时："+time;
        return response;
    }

    @RequestMapping("/hessianServiceBigMsg")
    public String hessianServiceBigMsg() {
        Long startTime = System.currentTimeMillis();
        List<User> users = null;
        try {
            HessianService helloHessian = HessianProxyFactoryUtil.getHessianClientBean(HessianService.class,host+"hessianTest");
            users =  helloHessian.bigMsg();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Long time = System.currentTimeMillis() - startTime;
        String response = "返回内容："+users.size()+"Hessian调用耗时："+time;
        return response;
    }

    @RequestMapping("/cxfServiceBigMsg")
    public String cxfServiceBigMsg() {
        Long startTime = System.currentTimeMillis();
        List<User> users = null;
        try{
            CxfService cxfService = CxfProxyFactoryUtil.getCxfClientBean(CxfService.class,host+"soap/user?wsdl");
            users = cxfService.bigMsg();
        }catch (Exception e){
            e.printStackTrace();
        }
        Long time = System.currentTimeMillis() - startTime;
        String response = "返回内容："+users.size()+"Cxf调用耗时："+time;
        return response;
    }

    @RequestMapping("/restGetServiceBigMsg")
    public String restGetServiceBigMsg(){
        Long startTime = System.currentTimeMillis();
        List users = restService.getRestBigMsg(host+"transmission/getRestBigMsg");
        Long time = System.currentTimeMillis() - startTime;
        String response = "返回内容："+users.size()+"restGet调用耗时："+time;
        return response;
    }
}
